﻿<%@ WebHandler Language="C#" Class="newsprenext" %>

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Web;
using System.IO;
using System.Text;
using System.Collections;
using System.Net;
using System.Xml;
using BusinessLogic.ClassFactory;
using LitJson;
using System.Text.RegularExpressions;
using RichECMS;

public class newsprenext : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.AddHeader("Content-Type", "application/json; charset=UTF-8");
        Hashtable result = new Hashtable();
        string aid = HttpContext.Current.Request["newsno"] ?? "null";
        int thisaid = 0;

        if (IsNum(aid))
        {
            thisaid = int.Parse(aid);
        }
        else
        {
            result["preid"] = "-1";
            result["pretitle"] = "null";
            result["nextid"] = "-1";
            result["nexttitle"] = "null"; context.Response.Write(
            JsonMapper.ToJson(result));
            return;
        }

        try
        {
            using (ArtSecRelationFactory objAsFactory = new ArtSecRelationFactory())
            {
                int secid = 0;
                BusinessLogic.BusinessRules.ArtSecRelation objArtSecRelation = objAsFactory.CreatorBySql(" intSecArtID=" + thisaid + " ");
                if (objArtSecRelation != null)
                {
                    secid = objArtSecRelation.DataItem.intSectionID;
                }
                else
                {
                    result["preid"] = "-1";
                    result["pretitle"] = "null";
                    result["nextid"] = "-1";
                    result["nexttitle"] = "null"; context.Response.Write(
                    JsonMapper.ToJson(result));
                    return;

                }

                StringBuilder strWhere = new StringBuilder();
                strWhere.Append("select ArtSecRelation.* from Article , ArtSecRelation   where Article.intArticleID = ArtSecRelation.intArticleID and ArtSecRelation.intSectionID=" + secid + " order by ArtSecRelation.intSort desc ,Article.dtmInTime desc,ArtSecRelation.intSecArtID desc");
                /*
                strWhere.Append("select * from (select top " + intPageSize + " * from  (select Top " + (intPageFrom).ToString() + " * from (select Top " + (intPageFrom).ToString() + " Article.* from Article inner  join ArtSecRelation on Article.intArticleID = ArtSecRelation.intArticleID and Article.intState = '" + Request["type"].ToString().Trim().Replace("'","")+ "' " + " where ArtSecRelation.intSectionID=" + State.intSTID.ToString() + State.strWhereSql + " order by Article.intSort desc ,Article.dtmInTime desc,Article.intArticleID desc)t order by intSort asc, dtmInTime asc ,intArticleID asc)m " + " order by intSort asc, dtmInTime asc ,intArticleID asc)mm  order by intSort desc, dtmInTime desc ,intArticleID desc");
                */
                objArtSecRelation = objAsFactory.CreatorByFullSql(strWhere.ToString());
                if (objArtSecRelation != null)
                {
                    //List<Hashtable> reslist = new List<System.Collections.Hashtable>();
                    for (int i = 0; i < objArtSecRelation.Count; i++)
                    {
                        if (objArtSecRelation.DataItem.intSecArtID == thisaid)
                        {
                            objArtSecRelation.DataItem.intAccessCount = objArtSecRelation.DataItem.intAccessCount + 1;
                            result["readcount"] = objArtSecRelation.DataItem.intAccessCount;
                            objArtSecRelation.Update();
                            if (i == 0)
                            {
                                result["preid"] = "-1";
                                result["pretitle"] = "null";

                                if (objArtSecRelation.Count > 1)
                                {
                                    objArtSecRelation.SelectRow(i + 1);
                                    result["nextid"] = objArtSecRelation.DataItem.intSecArtID;
                                    ArticleFactory objArticleFactory = new ArticleFactory();
                                    BusinessLogic.BusinessRules.Article objArticle = objArticleFactory.Creator(objArtSecRelation.DataItem.intArticleID);
                                    if (objArticle != null)
                                    {
                                        result["nexttitle"] = objArticle.DataItem.chvTitle;
                                    }
                                    else
                                    {
                                        result["nexttitle"] = "不存在相关信息";
                                    }
                                    objArticleFactory.Dispose();
                                }
                                else
                                {
                                    result["nextid"] = "-1";
                                    result["nexttitle"] = "null";
                                }
                            }
                            else if (i == (objArtSecRelation.Count - 1))
                            {
                                result["nextid"] = "-1";
                                result["nexttitle"] = "null";
                                objArtSecRelation.SelectRow(i - 1);

                                result["preid"] = objArtSecRelation.DataItem.intSecArtID;
                                ArticleFactory objArticleFactory = new ArticleFactory();
                                BusinessLogic.BusinessRules.Article objArticle = objArticleFactory.Creator(objArtSecRelation.DataItem.intArticleID);
                                if (objArticle != null)
                                {
                                    result["pretitle"] = objArticle.DataItem.chvTitle;
                                }
                                else
                                {
                                    result["pretitle"] = "不存在相关信息";
                                }
                                objArticleFactory.Dispose();
                            }
                            else
                            {
                                objArtSecRelation.SelectRow(i - 1);
                                result["preid"] = objArtSecRelation.DataItem.intSecArtID;
                                ArticleFactory objArticleFactory = new ArticleFactory();
                                BusinessLogic.BusinessRules.Article objArticle = objArticleFactory.Creator(objArtSecRelation.DataItem.intArticleID);
                                if (objArticle != null)
                                {
                                    result["pretitle"] = objArticle.DataItem.chvTitle;
                                }
                                else
                                {
                                    result["pretitle"] = "不存在相关信息";
                                }

                                objArtSecRelation.SelectRow(i + 1);
                                result["nextid"] = objArtSecRelation.DataItem.intSecArtID;
                                objArticle = objArticleFactory.Creator(objArtSecRelation.DataItem.intArticleID);
                                if (objArticle != null)
                                {
                                    result["nexttitle"] = objArticle.DataItem.chvTitle;
                                }
                                else
                                {
                                    result["nexttitle"] = "不存在相关信息";
                                }
                                objArticleFactory.Dispose();
                            }
                            break;
                        }

                        objArtSecRelation.SelectRow(i + 1);
                    }
                }

            }

            context.Response.Write(
           JsonMapper.ToJson(result));
        }
        catch (Exception error)
        {
        }
    }
    /// <summary>
    /// 检测是否能转化为数字
    /// </summary>
    /// <param name="param">检测字符串</param>
    /// <returns>输出true/false</returns>
    public static bool IsNum(string param)
    {
        const string regEx = @"^[0-9]*$";
        if (Regex.IsMatch(param, regEx))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    public static string CheckStr(string html)
    {
        System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" no[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</frameset *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"\<img[^\>]+\>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"</p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"<p>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
        html = regex1.Replace(html, ""); //过滤<script></script>标记 
        html = regex2.Replace(html, ""); //过滤href=javascript: (<A>) 属性 
        html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件 
        html = regex4.Replace(html, ""); //过滤iframe 
        html = regex5.Replace(html, ""); //过滤frameset 
        html = regex6.Replace(html, ""); //过滤frameset 
        html = regex7.Replace(html, ""); //过滤frameset 
        html = regex8.Replace(html, ""); //过滤frameset 
        html = regex9.Replace(html, "");
        html = html.Replace(" ", "");
        html = html.Replace("</strong>", "");
        html = html.Replace("<br />", "");
        html = html.Replace("<br/>", "");
        html = html.Replace("<strong>", "");
        html = html.Replace("&nbsp;", "");
        return html;
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}